package com.h.heal.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.h.heal.common.MyPageInfo;
import com.h.heal.entity.Comment;
import com.h.heal.entity.User;
import com.h.heal.mapper.CommentMapper;
import com.h.heal.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<Comment> findAll() {
        return commentMapper.findAll();
    }

    @Override
    public void deleteById(int id) {
        commentMapper.deleteById(id);
    }

    @Override
    public List<Comment> findByComment_vid(String comment_vid) {
        return commentMapper.findByComment_vid(comment_vid);
    }

    @Override
    public List<Comment> findByComment_uid(String comment_uid) {
        return commentMapper.findByComment_uid(comment_uid);
    }

    @Override
    public String create(Comment comment) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        comment.setComment_timestamp(simpleDateFormat.format(date));
        commentMapper.create(comment);
        return "ok";
    }

    @Override
    public Comment findById(int id) {
        return commentMapper.findById(id);
    }


    @Override
    public MyPageInfo findBySearch(HttpServletRequest request) {
        int currentPageNum = 1;//起始页1
        int pageSize = 10;//每页几条数据
        String currentPageNumStr = request.getParameter("pageNum");//从requst中获得pageNum参数
        if (currentPageNumStr != null) {
            currentPageNum = Integer.parseInt(currentPageNumStr);//将字符串转换成Integer类型
        }
        String comment_uid = request.getParameter("comment_uid");
        String comment_vid = request.getParameter("comment_vid");


        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在SQL之前执行
        commentMapper.findBySearch(comment_uid, comment_vid);

        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getNavigateLastPage(), pageInfo.getTotal(), pageInfo.getList());


        myPageInfo.setSearch1(comment_uid);
        myPageInfo.setSearch2(comment_vid);

        return myPageInfo;
    }

    @Override
    public MyPageInfo findBySearchUser(HttpServletRequest request) {
        int currentPageNum = 1;//起始页1
        int pageSize = 10;//每页几条数据
        String currentPageNumStr = request.getParameter("pageNum");//从requst中获得pageNum参数
        if (currentPageNumStr != null) {
            currentPageNum = Integer.parseInt(currentPageNumStr);//将字符串转换成Integer类型
        }
        String comment_uid =  String.valueOf(((User) request.getSession().getAttribute("user_info")).getUser_uid());
        String comment_vid = request.getParameter("comment_vid");


        Page<Object> page = PageHelper.startPage(currentPageNum, pageSize);//必须在SQL之前执行
        commentMapper.findBySearch(comment_uid, comment_vid);

        PageInfo<Object> pageInfo = page.toPageInfo();
        MyPageInfo myPageInfo = new MyPageInfo(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getNavigateLastPage(), pageInfo.getTotal(), pageInfo.getList());




        return myPageInfo;
    }

}
